/*
 * this class contains all operations needed to deal with users. It extends
 * DataAccessLayer class which contains more general methods to handle all
 * kind of beans and tables.
 *
 */
package webapp.model;

import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author Nina
 */
public class UserManager extends DataAccessLayer
{

    Connection connection;

    public User getUserInfo(String name) {

        User user = new User();
        try {
            user = getUserByUserName(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;

    }

    public String getUserEmail(int userId) {

        User user = new User();

        try {
            user = getUserByID(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user.getEmail();

    }



        /*
     * updateUserPassword
     * @param u - Take the user password in the object u and update the db.
     * The User id is used to determine which user, but all other fields are ignored.
     * @return - true if successful, false otherwise
     */
    public boolean updateUserPassword(User u) {

      //  String encryptedPword = DataAccessLayer.encryptPass(u.getUserPass());
      //  u.setUserPass(encryptedPword);
    	  u.setUserPass(u.getUserPass());

        boolean result = false;
        try {
            result = beanUpdateEngine(u, "WHERE id=" + u.getUserId());
            System.out.println("UserManager: attempted update of password: "+result);
        } catch (Exception e) {
            System.out.println("UserManager: Error updating User image.");
            result = false;
        }
        return result;
    }

    public void updateUserInfo(int userId, String userPass, String userEmail) {
        try {

            Statement statement = connection.createStatement();

            String querySQL_pass = "UPDATE User SET UserPass=\'" + userPass + "\' WHERE user.ID=" + userId;
            ;
            String querySQL_email = "UPDATE User SET email=\'" + userEmail + "\' WHERE user.ID=" + userId;

            if (userPass != null && !userPass.equals("")) {
                statement.executeQuery(querySQL_pass);
            }
            if (userEmail != null && !userEmail.equals("")) {
                statement.executeQuery(querySQL_email);
            }

            statement.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*   public String getUserNameByID(int id) {
        String user = null;
        try {
            user = getUserNameByID(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

   public User getUserByID(int id) {
        return getUserByID(id);
    } */


  
    public boolean validatePass(String name, String pass){
        User u = getUserByUserName(name);
        /* disable encrypted password
         String encryptedPass = DataAccessLayer.encryptPass(pass);
        if(u.getUserPass().equals(encryptedPass))
            return true;    
        */
        
        if(u.getUserPass().equals(pass))
        	return true;
        return false;
    }

    /*
     * addUser
     * Adds a User to the DB using the DAL
     * @param u - User to be added. ID of User object will be updated during insertion.
     * @returns the User with its modified ID based on insertion in the DB.
     */
    public User addUser(User u) {

       // String encryptedPword = DataAccessLayer.encryptPass(u.getUserPass());
        //u.setUserPass(encryptedPword);
    	u.setUserPass(u.getUserPass());

        int result = -1;
        try {
            result = insertUser(u);
        } catch (Exception e) {
            //should have logging code to report this error
        }
        u.setUserId(result);
        return u;
    }

    public User addUser(String name, String pword, String email) {        
        return (addUser(new User(name, pword, email)));
    }




} //end of the class

